package com.itheima.service.impl;

import com.itheima.mapper.MemberMapper;
import com.itheima.common.pojo.Member;
import com.itheima.common.pojo.Order;
import com.itheima.common.pojo.OrderSetting;
import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrderMapper;
import com.itheima.common.pojo.Reservation;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 体检预约
     * @param reservation
     */
    @Override
    public void submitOrder(Reservation reservation) throws Exception {
        log.info("开始处理体检预约: {}", reservation);
        // 1. 检查预约日期是否可以预约
        Date orderDate = DateUtils.parseString2Date(reservation.getOrderDate());
        OrderSetting orderSetting = orderMapper.findByOrderDate(orderDate);
        if (orderSetting == null) {
            throw new RuntimeException("所选日期不能进行体检预约");
        }

        // 2. 检查预约日期是否已经约满
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            throw new RuntimeException("所选日期预约已满");
        }

        // 3. 检查是否重复预约
        Integer setmealId = Integer.parseInt(reservation.getSetmealId());
        Order existOrder = orderMapper.findByCondition(
            orderDate,
            setmealId
        );
        if (existOrder != null) {
            throw new RuntimeException("不能重复预约");
        }

        // 4. 检查是否是会员，如果不是则自动完成注册
        Member member = memberMapper.findByTelephone(reservation.getTelephone());
        if (member == null) {
            member = new Member();
            member.setName(reservation.getName());
            member.setPhoneNumber(reservation.getTelephone());
            member.setIdCard(reservation.getIdCard());
            member.setSex(reservation.getSex());
            member.setRegTime(new Date());
            memberMapper.add(member);
        }

        // 5. 保存预约信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        order.setName(reservation.getName());
        order.setSex(reservation.getSex());
        order.setTelephone(reservation.getTelephone());
        order.setValidateCode(reservation.getValidateCode());
        
        orderMapper.submitOrder(order);
        log.info("预约订单已创建: {}", order);

        // 6. 更新已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderMapper.updateReservations(orderSetting);
        log.info("已更新预约人数: {}", orderSetting);
    }
}
